<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
            <title>proxy</title>
            <style>
                .hidden {
                    display: none;
                    background: none;
                }
            </style>
    </head>
    <body style="display: block;background:none;width: 100%;">
        <iframe id="BPMForm" class=""></iframe>
        <script>
            var BPMForm = document.getElementById('BPMForm');
            var BPMFormWindow = BPMForm.contentWindow;
            
            // 获取状态
            function setStatus(status) {
                Dist.getStatus(JSON.stringify({status: status}));
            }
        
        // 外层页面加载完毕
        window.onload = function () {
            setStatus('loadSuccess');
        };
        
        // 加载完整表单
        function loadForm(url) {
            BPMForm.src = url;
            setStatus('loadForm');
        }
        
        // 完整表单加载完毕
        BPMForm.onload = function () {
            setStatus('loadFormSuccess');
        };
        
        // 注销表单
        function removeCoach() {
            BPMForm.src = '';
        }
        
        /***************** 给BPM发消息 *******************/
        
        // 发消息
        function postMsg(messageType) {
            BPMFormWindow.postMessage("{\"type\":\"" + messageType + "\"}", "*");
        }
        
        function onMessage(name, callback) {
            function handleMessage($event) {
                // alert($event.data);
                var _temp = $event.data.replace('undefined', '""');
                var data = JSON.parse(_temp);
                if (data.type == name || data.name == name) {
                    callback(data);
                    window.removeEventListener('message', handleMessage, false);
                }
            }
            
            window.addEventListener('message', handleMessage, false);
        }
        
        // 保存完整表单
        function postSaveFormMessage() {
            
            postMsg('saveForm');
            setStatus('postSaveFormMessage');
            
            onMessage('saveSuccess', function (result) {
                      //alert('完整表单保存结果：' + JSON.stringify(result));
                      setStatus('onSaveFormSuccess');
                      });
        }
        
        // 验证完整表单
        function postValidateFormMessage() {
            
            postMsg('validateProject');
            setStatus('postValidateFormMessage');
            
            onMessage('validateResult', function (data) {
                      //alert('表单验证结果：' + JSON.stringify(data));
                      setStatus('表单验证结果：' + JSON.stringify(data));
                      if (data.result == 'true' || data.result == '') {
                      //alert('验证通过');
                      setStatus('onValidateFormSuccess');
                      } else {
                      //alert('验证不通过');
                      setStatus('onValidateFormFailure');
                      }
                      });
        }
        
        // 发送完整表单
        function postSendFormMessage(arg) {
            
            postMsg('ProjectSend');
            setStatus('postSendMessage');
            
            onMessage('onCompleted', function (result) {
                      //alert('完整表单发送结果：' + JSON.stringify(result));
                      setStatus('onSendFormSuccess');
                      removeCoach();
                      });
        }
        
        // 未办结发送完整表单
        function postNotCompleteSendMessage(arg) {
            
            postMsg('notcompletedSend');
            setStatus('notcompletedSend');
            
            onMessage('notcompletedSendSuccess', function (result) {
                      //alert('完整表单发送结果：' + JSON.stringify(result));
                      setStatus('notcompletedSendSuccess');
                      removeCoach();
                      });
        }
        
        
            </script>
    </body>
</html>
